import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class GetCurrentWifi {

    public static void main(String[] args) {
        String wifiName = getCurrentWifiName();
        if (wifiName != null) {
            System.out.println("当前连接的 Wi-Fi 网络: " + wifiName);
        } else {
            System.out.println("当前没有连接 Wi-Fi");
        }
    }

    public static String getCurrentWifiName() {
        ProcessBuilder processBuilder = new ProcessBuilder("/bin/bash", "-c", "/usr/sbin/networksetup -getairportnetwork en0 | awk -F': ' '{print $2}'");
        Process process;
        try {
            process = processBuilder.start();
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                return line.trim();
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
